import { connect } from 'dva'

import AekButton from '@/components/AekButton'
import AekLinkButton from '@/components/AekLinkButton'
import AekTable from '@/components/AekTable'
import PageLayout from '@/components/PageLayout'
import { namespace } from '@/models/pcs/cyCategory'
import { getPagination, message } from '@/utils/antdHelper'
import { getBasicFn } from '@/utils/dvaUtil/index'

import EditModal from './components/EditModal'

/** 陪护 - 商品分类管理 */
const Index = ({ modelState, loading }) => {
  const { pageList, pagination } = modelState
  const { toAction, getLoading } = getBasicFn({ namespace, loading })

  const add = async () => {
    await EditModal.PromiseThen()
    message.success('新增成功')
    toAction('getPageList')
  }

  const edit = async row => {
    await EditModal.PromiseThen({
      row,
    })
    message.success('修改成功')
    toAction('getPageList')
  }

  /** @type {import("@/components/AekTable").AekTableProps} */
  const tableProps = {
    configKey: namespace,
    rowKey: 'id',
    loading: getLoading('getPageList'),
    dataSource: pageList,
    pagination: getPagination(pagination, (current, pageSize) => {
      toAction('getPageList', { current, pageSize })
    }),
    columns: [
      { dataIndex: 'name', title: '分类名称' },
      { dataIndex: 'commodityQuantity', title: '商品数量' },
      {
        key: 'operate',
        title: '操作',
        fixed: 'right',
        width: 100,
        className: 'text-center!',
        render: (_, row) => (
          <AekLinkButton permissionKey={`${namespace}:update`} onClick={() => edit(row)}>
            编辑
          </AekLinkButton>
        ),
      },
    ],
  }

  return (
    <PageLayout
      extraSuffix={
        <AekButton type='primary' onClick={add}>
          新建
        </AekButton>
      }
    >
      <AekTable {...tableProps} />
    </PageLayout>
  )
}

export default connect(store => ({
  modelState: store[namespace],
  loading: store.loading,
}))(Index)
